package com.moonstar;

import javax.swing.*;
import javax.swing.event.AncestorListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class work2 {

    public static Integer index1 = 0;
    public static Integer index2 = 0;
    public static Integer index3 = 0;
    public static Integer index4 = 0;
    public static String Lock = "lock3";
    public static String Lock4="lock4";


    public static void main(String[] args) {
        //构建jframe
        JFrame jf = new JFrame("Thread");

        //流式布局，左对齐
        jf.setLayout(new FlowLayout(FlowLayout.LEFT));
        jf.setBounds(500, 500, 527, 169);

        //JTextArea 文本框
        JTextArea jt1 = new JTextArea(1, 40);
        JTextArea jt2 = new JTextArea(1, 40);
        JTextArea jt3 = new JTextArea(1, 40);
        JTextArea jt4 = new JTextArea(1, 40);

        JButton jb1 = new JButton("创建线程");
        myThread1 thread1 = new myThread1(jt1);
        myThread2 thread2 = new myThread2(jt2);
        myThread3 thread3 = new myThread3(jt3);
        myThread4 thread4 = new myThread4(jt3);
        myThread5 thread5 = new myThread5(jt4);
        myThread6 thread6 = new myThread6(jt4);

        jb1.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(thread1.isAlive()){
                    thread1.resume();
                }if(thread2.isAlive()){
                    thread2.resume();
                }else{
                    thread1.start();
                    thread2.start();
                }
            }
        });

        JButton jb2 = new JButton("线程互斥");

        jb2.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(thread3.isAlive()){
                    thread3.resume();
                }if(thread4.isAlive()){
                    thread4.resume();
                }else{
                    thread3.start();
                    thread4.start();
                }
            }
        });

        JButton jb3 = new JButton("非互斥运行");
        jb3.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if(thread5.isAlive()){
                    thread5.resume();
                }if(thread6.isAlive()){
                    thread6.resume();
                }else{
                    thread5.start();
                    thread6.start();
                }
            }
        });

        JButton jb4 = new JButton("结束线程");
        jb4.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                thread1.suspend();
                thread2.suspend();
                thread3.suspend();
                thread4.suspend();
                thread5.suspend();
                thread6.suspend();
            }
        });

        JButton jb5 = new JButton("退出");

        jb5.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        jf.add(jt1);
        jf.add(jt2);
        jf.add(jt3);
        jf.add(jt4);
        jf.add(jb1);
        jf.add(jb2);
        jf.add(jb3);
        jf.add(jb4);
        jf.add(jb5);
        jf.setVisible(true);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);


    }

}
